package com.jerry.walking.module.activityPage.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.walking.R;
import com.jerry.walking.base.BaseActivity;
import com.jerry.walking.constant.Urls;
import com.jerry.walking.module.activityPage.adapter.ActivityItemAdapter;
import com.jerry.walking.module.activityPage.bean.Lore;
import com.jerry.walking.utils.EventMessage;
import com.jerry.walking.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/5/11 0011.
 */
public class CategorySearchActivity extends BaseActivity{

    @BindView(R.id.imageView)
    ImageView imageView;

    @BindView(R.id.editText)
    EditText editText;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.sp_city)
    Spinner spCity;
    @BindView(R.id.sp_sort)
    Spinner spSort;
    @BindView(R.id.pull_refer)
    PullToRefreshListView pullRefer;

    private int page_size = 20;
    private String keyword;
    private List<Lore.ActivitiesBean> mActivityList;
    String URL = Urls.CATEGORY_LIST;
    private ActivityItemAdapter mActivityAdapter;

    @OnClick(R.id.ll_back)
    void back(){
        finish();
    }

    @Override
    protected int getLayoutId(){
        return R.layout.activity_category_search;
    }

    @Override
    protected void initViews(){
        mActivityList= new ArrayList<>();
        mActivityAdapter = new ActivityItemAdapter(this,mActivityList);
        pullRefer.setAdapter(mActivityAdapter);

        Intent intent = getIntent();
        keyword = intent.getStringExtra("keyword");
        editText.setHintTextColor(Color.WHITE);
        if(!"全部".equals(keyword)){
            editText.setText(keyword);
        }

        pullRefer.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>(){
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView){
                mActivityList.clear();
                page_size=20;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView){
                page_size+=10;
                loadData();
            }
        });

    }

    @Override
    protected void loadData(){
        OkHttpUtils.get(URL+keyword+"&page_size="+page_size, new OkHttpUtils.OnGetDataListener(){
            @Override
            public void onResponse(String url, String data){

                EventBus.getDefault().post(new EventMessage(data,url));
            }

            @Override
            public void onFailure(String url, String error){
            }
        });
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message){
        if((URL+keyword+"&page_size="+page_size).equals(message.type)){
            Gson gson = new Gson();
            Lore lore = gson.fromJson(message.result, Lore.class);
            if(lore != null && lore.getActivities()!= null) {
                mActivityList.clear();
                mActivityList.addAll(lore.getActivities());
            }
            mActivityAdapter.notifyDataSetChanged();
        }
        pullRefer.onRefreshComplete();
    }
}
